package com.ph.base;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import com.ph.shopping.common.core.exception.BizException;

/**
 * @项目：phshopping-api-platform
 *
 * @描述：通用异常处理
 *
 * @作者： Mr.chang
 *
 * @创建时间：2017年3月27日
 *
 * @Copyright @2017 by Mr.chang
 */
@ControllerAdvice
public class BaseErrorController {
	
	/**
	 * 500异常处理
	 * @param request
	 * @param e
	 * @return
	 * @throws Exception
	 */
	@ExceptionHandler(value=Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) throws Exception {

            ModelAndView view = new ModelAndView();
            view.addObject("code", 500);
            view.addObject("msg", e.getMessage());
            view.addObject("url", request.getRequestURL());
            view.setViewName("error/500");
            return view;

    }
	
	/**
	 * 404异常处理
	 * @param request
	 * @param e
	 * @return
	 * @throws Exception
	 */
	@ExceptionHandler(value=NoHandlerFoundException.class)
    public ModelAndView pageNoFoundHandler(HttpServletRequest request, Exception e) throws Exception {
            ModelAndView view = new ModelAndView();
            view.addObject("code", 404);
            view.addObject("msg", "页面未找到");
            view.addObject("url", request.getRequestURL());
            view.setViewName("error/404");
            return view;
    }
	
	/**
	 * biz业务异常处理
	 * @param request
	 * @param e
	 * @return
	 * @throws Exception
	 */
	@ExceptionHandler(value=BizException.class)
	public ModelAndView bizExceptionHandler(HttpServletRequest request, BizException e) throws Exception {
		ModelAndView view = new ModelAndView();
		view.addObject("code", e.getCode());
		view.addObject("msg", e.getMsg());
		view.addObject("url", request.getRequestURL());
		view.setViewName("error/bizError");
		return view;
	}

}
